<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\admin\controller;

class Config extends \app\AdminRest
{
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
    }
    public function updateOssConfig()
    {
        $uniacid = $this->_uniacid;
        $input = [];
        if(isset($this->_input["oss_config"])) {
            $input = $this->_input["oss_config"];
        }
        $data = getOssConfigData($input);
        $data["uniacid"] = $uniacid;
        $oss_config_model = new \app\admin\model\OssConfig();
        $oss_config = $oss_config_model->getConfig(["uniacid" => $uniacid]);
        $result = false;
        $data["is_sync"] = 1;
        if(empty($oss_config)) {
            $result = $oss_config_model->createConfig($data);
        } else {
            $result = $oss_config_model->updateConfig(["uniacid" => $uniacid], $data);
        }
        $config = longbingGetOssConfig($uniacid, true);
        if(!empty($result) && !empty($data["open_oss"])) {
            $path = LONGBING_EXTEND_PATH . "timg.jpg";
            if(file_exists($path)) {
                $file = new \think\file\UploadedFile($path, "test.jpg");
                $file_upload_model = new \app\Common\Upload($uniacid);
                $check = $file_upload_model->upload("picture", $file);
                if(empty($check)) {
                    return $this->error(lang("upload config error"));
                }
            }
        }
        return $this->success($result);
    }
    public function getOssConfig()
    {
        $uniacid = $this->_uniacid;
        $oss_config_model = new \app\admin\model\OssConfig();
        $config = $oss_config_model->getConfig(["uniacid" => $uniacid]);
        if(!empty($config)) {
            unset($config["id"]);
        }
        return $this->success($config);
    }
    public function getAppConfig()
    {
        $uniacid = $this->_uniacid;
        $result = longbingGetAppConfig($uniacid);
        return $this->success($result);
    }
    public function updateAppConfig()
    {
    }
    public function setAppConfig()
    {
        $uniacid = $this->_uniacid;
        $input = NULL;
        if(isset($this->_input["app_config"])) {
            $input = $this->_input["app_config"];
        }
        if(empty($input)) {
            return $this->error("not app config data ,please check input data.");
        }
        $input["uniacid"] = $this->_uniacid;
        $result = longbingGetAppConfig($uniacid);
        $app_config_model = new \app\admin\model\AppConfig();
        $input["is_sync"] = 1;
        if(!empty($input["notice_switch"]) && $input["notice_switch"] == 4) {
            $insrt["yq_corpid"] = $input["yq_corpid"];
            $insrt["yq_corpsecret"] = $input["yq_corpsecret"];
            $insrt["yq_agentid"] = $input["yq_agentid"];
            $send_model = new \app\sendmsg\model\SendConfig();
            $data = $send_model->configUpdate(["uniacid" => $this->_uniacid], $insrt);
            unset($input["yq_corpid"]);
            unset($input["yq_corpsecret"]);
            unset($input["yq_agentid"]);
        }
        if(!isset($result["uniacid"]) || empty($result)) {
            $result = $app_config_model->createConfig($input);
        } else {
            $result = $app_config_model->updateConfig(["id" => $result["id"]], $input);
        }
        longbingGetAppConfig($uniacid, true);
        return $this->success($result);
    }
    public function autoServiceNoticeTemplate()
    {
        $config = longbingGetAppConfig($this->_uniacid);
        if(!isset($config["appid"]) || empty($config["appid"]) || !isset($config["app_secret"]) || empty($config["app_secret"])) {
            return $this->error("wx app site not exist ,please check site message.");
        }
        $ac = longbingSingleGetAccessTokenByUniacid($this->_uniacid);
        if(empty($ac)) {
            return $this->error(lang("wx app site error"));
        }
        $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=" . $ac;
        $data = ["id" => "AT1442", "keyword_id_list" => [4, 7, 1]];
        $data = json_encode($data);
        $result = longbingCurl($url, $data, "POST");
        $result = json_decode($result, true);
        if(isset($result["errcode"]) && $result["errcode"] == 40001) {
            $ac = longbingSingleGetAccessTokenByUniacid($this->_uniacid, true);
            $url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=" . $ac;
            $result = longbingCurl($url, $data, "POST");
            $result = json_decode($result, true);
        }
        if(isset($result["errcode"]) && !empty($result["errcode"])) {
            return $this->error(lang("auto get template error"));
        }
        $app_config_model = new \app\admin\model\AppConfig();
        $mini_template_id = $result["template_id"];
        $result = $app_config_model->updateConfig(["id" => $config["id"]], ["mini_template_id" => $mini_template_id]);
        if($result) {
            longbingGetAppConfig($this->_uniacid, true);
        }
        $result = ["mini_template_id" => $mini_template_id];
        return $this->success($result);
    }
    public function getTabbar()
    {
        $uniacid = $this->_uniacid;
        $result = longbingGetAppTabbar($uniacid, true);
        $result = longbingGetAppTabbarResponse($result);
        $pluginAuth = longbingGetPluginAuth($uniacid);
        $plugin_map = ["activity" => "activity", "appointment" => "appoint", "house" => "house"];
        $meta_map = ["card" => "BusinessCard", "shop" => "Malls", "dynamic" => "Dynamic", "website" => "Website"];
        foreach ($result["data"] as $k => $item) {
            if(in_array($k, array_keys($plugin_map)) && $pluginAuth["plugin"][$plugin_map[$k]] == 0) {
                unset($result["data"][$k]);
            } elseif(in_array($k, array_keys($meta_map)) && $pluginAuth["web_manage_meta_config"][$meta_map[$k]] == 0) {
                unset($result["data"][$k]);
            }
        }
        $result = array_merge($result, $pluginAuth);
        return $this->success($result);
    }
    public function setTabbar()
    {
        $uniacid = $this->_uniacid;
        $input = NULL;
        if(isset($this->_input["data"])) {
            $input = $this->_input["data"];
        }
        $input = longbingGetAppTabbarRequest($input);
        if(empty($input)) {
            return $this->error("not tabbar data");
        }
        $tabbar = longbingGetAppTabbar($uniacid);
        $menu_now = ["menu1_is_hide" => $tabbar["menu1_is_hide"], "menu2_is_hide" => $tabbar["menu2_is_hide"], "menu3_is_hide" => $tabbar["menu3_is_hide"], "menu4_is_hide" => $tabbar["menu4_is_hide"], "menu_appoint_is_hide" => $tabbar["menu_appoint_is_hide"], "menu_activity_is_show" => $tabbar["menu_activity_is_show"], "menu_house_is_show" => $tabbar["menu_house_is_show"]];
        $permissions = longbingGetPluginAuth($this->_uniacid);
        $not_tabbars = [];
        if(!empty($permissions) && !empty($permissions["plugin"])) {
            if(!isset($permissions["plugin"]["appoint"]) || empty($permissions["plugin"]["appoint"])) {
                $not_tabbars[] = "menu_appoint_is_hide";
                $input["menu_appoint_is_hide"] = 0;
            }
            if(!isset($permissions["plugin"]["activity"]) || empty($permissions["plugin"]["activity"])) {
                $not_tabbars[] = "menu_activity_is_show";
                $input["menu_activity_is_show"] = 0;
            }
            if(!isset($permissions["plugin"]["house"]) || empty($permissions["plugin"]["house"])) {
                $not_tabbars[] = "menu_house_is_show";
                $input["menu_house_is_show"] = 0;
            }
            if(!isset($permissions["web_manage_meta_config"]["Website"]) || empty($permissions["web_manage_meta_config"]["Website"])) {
                $not_tabbars[] = "menu4_is_hide";
                $input["menu4_is_hide"] = 0;
            }
            if(!isset($permissions["web_manage_meta_config"]["Malls"]) || empty($permissions["web_manage_meta_config"]["Malls"])) {
                $not_tabbars[] = "menu2_is_hide";
                $input["menu3_is_hide"] = 0;
            }
            if(!isset($permissions["web_manage_meta_config"]["Dynamic"]) || empty($permissions["web_manage_meta_config"]["Dynamic"])) {
                $not_tabbars[] = "menu3_is_hide";
                $input["menu3_is_hide"] = 0;
            }
        }
        $max_tabbar_count = env("MAX_TABBAR_COUNT", 5);
        $all_tabbar_count = 0;
        foreach ($menu_now as $k => $v) {
            if(isset($input[$k])) {
                $v = $input[$k];
            }
            if(!in_array($k, $not_tabbars)) {
                $all_tabbar_count = $all_tabbar_count + $v;
            }
        }
        if($max_tabbar_count < $all_tabbar_count) {
            return $this->error("显示的菜单栏不能大于 " . $max_tabbar_count);
        }
        $result = false;
        $tabbar_model = new \app\admin\model\AppTabbar();
        if(empty($tabbar)) {
            $input["uniacid"] = $uniacid;
            $result = $tabbar_model->createTabbar($input);
        } else {
            $result = $tabbar_model->updateTabbar(["id" => $tabbar["id"]], $input);
        }
        longbingGetAppTabbar($uniacid, true);
        return $this->success($result);
    }
    public function clearCache()
    {
        $uniacid = $this->_uniacid;
        \app\admin\service\UpdateService::installSql(8888, 0);
        $a = new \longbingcore\wxcore\WxSetting($this->_uniacid);
        $a->setH5Info();
        $config_model = new \app\massage\model\ConfigSetting();
        $config_model->initData($uniacid);
        $broker_model = new \app\coachbroker\model\CoachBroker();
        $broker_model->initBroker($uniacid);
        $config_model = new \app\massage\model\BtnConfig();
        $config_model->initData($uniacid);
        $order_model = new \app\massage\model\Order();
        $order_model->initOrderData($uniacid);
        $order_model->initCoachRefundOrder($uniacid);
        $refund_model = new \app\massage\model\RefundOrder();
        $refund_model->initRefundOrderData($uniacid);
        $coach_model = new \app\massage\model\Coach();
        $free_fare_bear = getConfigSetting($uniacid, "free_fare_bear");
        $key = "free_fare_bear_key";
        if(!getCache($key, 3333) && 0 < $free_fare_bear) {
            $coach_model->where(["admin_id" => 0])->update(["free_fare_bear" => $free_fare_bear]);
            setCache($key, 1, 0, 3333);
        }
        $result = @clearCache($uniacid);
        @clearCache($this->_uniacid, "city_key");
        return $this->success($result);
    }
    public function wxUploadInfo()
    {
        $model = new \app\admin\model\WxUpload();
        $dis = ["uniacid" => $this->_uniacid];
        $data = $model->settingInfo($dis);
        $data["app_id"] = !empty($data["app_id"]) ? explode(",", $data["app_id"]) : [];
        return $this->success($data);
    }
    public function wxUploadUpdate()
    {
        $input = $this->_input;
        $model = new \app\admin\model\WxUpload();
        $dis = ["uniacid" => $this->_uniacid];
        $data = $model->settingUpdate($dis, $input);
        return $this->success($data);
    }
}

?>